package ucm;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import model.Database_Model;
import view.Air_Register;

public class UCCheckUser {
    String username;

    UCCheckUser() {
    }

    public UCCheckUser(String username) {
        this.username = username;
    }

    public UCCheckUser(Air_Register user) {
        this.username = user.getName();
    }

    public String run() {
        boolean user_exist = true;

        try {
            Connection con = new Database_Model().getConnection();
            Statement stmt = con.createStatement();

            String User = "select * from user where Name='" + this.username + "'";
            ResultSet res = stmt.executeQuery(User);
            if (res.first()&&!res.wasNull()) {
                user_exist = true;
            } else {
                user_exist = false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if (user_exist) {
            return "The user name already exists.";
        } else {
            return "The user name has not been used.";
        }
    }
}
